package com.zhome.gateway.interceptor;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.zhome.gateway.model.Result;
import com.zhome.gateway.util.DesEncrypt;
import com.zhome.gateway.util.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Slf4j
//@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public String exceptionHandler(HttpServletRequest req, Exception e)  {
        log.error("未知异常！原因是:", e);
        try {
            return DesEncrypt.encrypt(JsonUtil.toJsonString(Result.innerError("内部错误")));
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }
}
